package com.loongtech.bi.advertisment.controller;

import com.loongtech.bi.action.TransferHelper;
import com.loongtech.bi.entity.system.EntityAdvConfig;
import com.loongtech.bi.manager.system.AdvConfigManager;
import com.loongtech.bi.manager.system.SysRoleAdManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.util.*;

public abstract class BaseAdvAction extends TransferHelper {
	private static final long serialVersionUID = 1L;
	@Autowired
	private AdvConfigManager advConfigManager;

	@Autowired
	private SysRoleAdManager sysRoleAdManager;

	@Autowired
	Session session;

	private List<EntityAdvConfig> list;

	protected Date lookDate = new Date(Time.getBeginningOfDay(Time.currentTime())); // 前端传过来的查看时间
	// 广告url
	private List<String> id;
	protected String[] selectedId;
	// 归属
	private List<String> affiliation;
	protected String[] selectedAffiliation;
	// 渠道名称
	private List<String> channelName;
	protected String[] selectedChannelName;
	// 代理公司
	private List<String> agent;
	protected String[] selectedAgent;
	// 平台
	private List<String> platform;
	protected String[] selectedPlatform;
	// 结算方式
	private List<String> paytype;
	protected String[] selectedPaytype;
	// 广告位置
	private List<String> advPosition;
	protected String[] selectedAdvPosition;

	protected Set<String> hasAgents; // 用户拥有的代理商权限

	public List<String> getId() {
		return id;
	}

	public void setId(List<String> id) {
		this.id = id;
	}

	public String[] getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String[] selectedId) {
		this.selectedId = selectedId;
	}

	public List<String> getAffiliation() {
		return affiliation;
	}

	public void setAffiliation(List<String> affiliation) {
		this.affiliation = affiliation;
	}

	public String[] getSelectedAffiliation() {
		return selectedAffiliation;
	}

	public void setSelectedAffiliation(String[] selectedAffiliation) {
		this.selectedAffiliation = selectedAffiliation;
	}

	public List<String> getChannelName() {
		return channelName;
	}

	public void setChannelName(List<String> channelName) {
		this.channelName = channelName;
	}

	public String[] getSelectedChannelName() {
		return selectedChannelName;
	}

	public void setSelectedChannelName(String[] selectedChannelName) {
		this.selectedChannelName = selectedChannelName;
	}

	public List<String> getAgent() {
		return agent;
	}

	public void setAgent(List<String> agent) {
		this.agent = agent;
	}

	public String[] getSelectedAgent() {
		return selectedAgent;
	}

	public void setSelectedAgent(String[] selectedAgent) {
		this.selectedAgent = selectedAgent;
	}

	public List<String> getPlatform() {
		return platform;
	}

	public void setPlatform(List<String> platform) {
		this.platform = platform;
	}

	public String[] getSelectedPlatform() {
		return selectedPlatform;
	}

	public void setSelectedPlatform(String[] selectedPlatform) {
		this.selectedPlatform = selectedPlatform;
	}

	public List<String> getPaytype() {
		return paytype;
	}

	public void setPaytype(List<String> paytype) {
		this.paytype = paytype;
	}

	public String[] getSelectedPaytype() {
		return selectedPaytype;
	}

	public void setSelectedPaytype(String[] selectedPaytype) {
		this.selectedPaytype = selectedPaytype;
	}

	public List<String> getAdvPosition() {
		return advPosition;
	}

	public void setAdvPosition(List<String> advPosition) {
		this.advPosition = advPosition;
	}

	public String[] getSelectedAdvPosition() {
		return selectedAdvPosition;
	}

	public void setSelectedAdvPosition(String[] selectedAdvPosition) {
		this.selectedAdvPosition = selectedAdvPosition;
	}

	public Date getLookDate() {
		return lookDate;
	}

	public void setLookDate(Date lookDate) {
		this.lookDate = lookDate;
	}

	public Set<String> getHasAgents() {
		return hasAgents;
	}

	public void setHasAgents(Set<String> hasAgents) {
		this.hasAgents = hasAgents;
	}

	/**
	 *
	 * @Description: TODO 初始化选择条件
	 * @param
	 * @return void
	 * @throws
	 * @author liuchen
	 */
	@PostConstruct
	public void initData() {
		advConfigManager.init();
		list = AdvConfigManager.ADV_CONFIG_LIST;
		id = new ArrayList<String>();
		affiliation = new ArrayList<String>();
		channelName = new ArrayList<String>();
		agent = new ArrayList<String>();
		platform = new ArrayList<String>();
		paytype = new ArrayList<String>();
		advPosition = new ArrayList<String>();

		hasAgents = sysRoleAdManager.getAdSetByUserId(session.getUserId());

		for (EntityAdvConfig adv : list) {
			if (!id.contains(adv.getId()))
				id.add(adv.getId());
			if (!affiliation.contains(adv.getAffiliation()))
				affiliation.add(adv.getAffiliation());
			if (!channelName.contains(adv.getChannelName()))
				channelName.add(adv.getChannelName());
			//if (!agent.contains(adv.getAgent()) && hasAgents.contains(adv.getAgent()))
			if (!agent.contains(adv.getAgent()))
				agent.add(adv.getAgent());
			if (!platform.contains(adv.getPlatform()))
				platform.add(adv.getPlatform());
			if (!paytype.contains(adv.getPaytype()))
				paytype.add(adv.getPaytype());
			if (!advPosition.contains(adv.getAdvPosition()))
				advPosition.add(adv.getAdvPosition());
		}

		selectedAgent = agent.toArray(new String[agent.size()]);
	}

    public void initData(int userid) {
        advConfigManager.init();
        list = AdvConfigManager.ADV_CONFIG_LIST;
        id = new ArrayList<String>();
        affiliation = new ArrayList<String>();
        channelName = new ArrayList<String>();
        agent = new ArrayList<String>();
        platform = new ArrayList<String>();
        paytype = new ArrayList<String>();
        advPosition = new ArrayList<String>();

        hasAgents = sysRoleAdManager.getAdSetByUserId(userid);

        if (hasAgents.size() <= 0) {
            return;
        }

        for (EntityAdvConfig adv : list) {
            if (!hasAgents.contains(adv.getAgent())) {
                continue;
            }

            if (!id.contains(adv.getId()))
                id.add(adv.getId());
            if (!affiliation.contains(adv.getAffiliation()))
                affiliation.add(adv.getAffiliation());
            if (!channelName.contains(adv.getChannelName()))
                channelName.add(adv.getChannelName());
            //if (!agent.contains(adv.getAgent()) && hasAgents.contains(adv.getAgent()))
            if (!agent.contains(adv.getAgent()))
                agent.add(adv.getAgent());
            if (!platform.contains(adv.getPlatform()))
                platform.add(adv.getPlatform());
            if (!paytype.contains(adv.getPaytype()))
                paytype.add(adv.getPaytype());
            if (!advPosition.contains(adv.getAdvPosition()))
                advPosition.add(adv.getAdvPosition());
        }

        //selectedAgent = agent.toArray(new String[agent.size()]);
    }
}
